package com.etung.iot.demo.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etung.iot.demo.dao.RealDataMapper;
import com.etung.iot.demo.dao.StorageMapper;
import com.etung.iot.demo.modles.RealData;
import com.etung.iot.demo.modles.StorageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class StorageRuleUtil {
    @Autowired
    private StorageMapper storageMapper;
    @Autowired
    private RealDataMapper realDataMapper;

    //生产环境请使用redis
    private static Map<String, StorageEntity> storageMap = new ConcurrentHashMap<>();
    //周期存储的上次存储时间
    private static Map<String,Long> lastStoreMap = new ConcurrentHashMap<>();
    //范围存储的上次值
    private static Map<String,String> lastValueStoreMap = new ConcurrentHashMap<>();

    public StorageEntity getStorageRule(String imei,Integer varId){
        String key = imei + "_"+varId;
        StorageEntity storage = null;
        if (!storageMap.containsKey(key)){
            storage = storageMapper.selectOne(
                    new QueryWrapper<StorageEntity>()
                            .eq("imei", imei)
                            .eq("var_id",varId)
                            );
            if (storage != null)
                storageMap.put(key,storage);
        }else{
            storage = storageMap.get(key);
        }
        return storage;
    }

    public void removeStorageRule(String imei){
        storageMap.entrySet().removeIf(entry -> entry.getKey().startsWith(imei));
        lastStoreMap.entrySet().removeIf(entry -> entry.getKey().startsWith(imei));
        lastValueStoreMap.entrySet().removeIf(entry -> entry.getKey().startsWith(imei));
    }

    public Long getLastStoreTimestamp(String imei,Integer varId){
        String key = imei + "_"+varId;
        Long res = 0L;
        if (lastStoreMap.containsKey(key)){
            res = lastStoreMap.get(key);
        }else{
            RealData realData = realDataMapper.selectOne(
                    new QueryWrapper<RealData>()
                            .eq("imei", imei).eq("variant_id", varId)
                            .orderByDesc("`time`").last("limit 1")

            );
            if (realData != null){
                res = realData.getTime().getTime();
                lastStoreMap.put(key,res);
            }
        }
        return res;
    }

    public void saveLastStoreTimestamp(String imei,Integer varId,Long timestamp){
        String key = imei + "_"+varId;
        lastStoreMap.put(key,timestamp);
    }

    public String getLastStoreValue(String imei,Integer varId){
        String key = imei + "_"+varId;
        String res = "";
        if (lastValueStoreMap.containsKey(key)){
            res = lastValueStoreMap.get(key);
        }else{
            RealData realData = realDataMapper.selectOne(
                    new QueryWrapper<RealData>()
                            .eq("imei", imei).eq("variant_id", varId)
                            .orderByDesc("`time`").last("limit 1")

            );
            if (realData != null){
                res = realData.getValue();
                lastValueStoreMap.put(key,res);
            }
        }
        return res;
    }
    public void saveLastStoreValue(String imei,Integer varId,String value){
        String key = imei + "_"+varId;
        lastValueStoreMap.put(key,value);
    }
}
